package com.zc58s.springbootmqtt.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zc58s.springbootmqtt.mqtt.MqttBundle;
import com.zc58s.springbootmqtt.mqtt.Webtalk;
import com.zc58s.springbootmqtt.mqtt.vo.Dataarr;
import com.zc58s.springbootmqtt.mqtt.vo.PublishUpdateTagValReq;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author : fujc-dev@qq.com
 * @created : 2022/9/23 11:09
 */
@Controller
@ResponseBody
@RequestMapping
public class CommonController {

    @Value("${kggs.mqtt.wbtksn}")
    private String wbtksn;
    @Value("${kggs.mqtt.cloudsn}")
    private String cloudsn;
    @Autowired
    private MqttBundle mqttBundle;

    private int sn = 0;

    @SneakyThrows
    @ApiOperation(value = "更新点位", notes = "更新点位")
    @PostMapping("/update")
    public Map<String, Object> update() {
        System.out.println("当前线程：" + Thread.currentThread().getId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        sn++;
        PublishUpdateTagValReq res = new PublishUpdateTagValReq();
        res.setSn(sn);
        res.setWbtksn(wbtksn);
        res.setCloudsn(cloudsn);
        Random random = new Random();
        int val = random.nextInt(100);
        res.setDataarr(new Dataarr("C_1001_VR_0000", val));
        ObjectMapper mapper = new ObjectMapper();
        String req = mapper.writeValueAsString(res);
        System.out.println("CSN/WSN/pub/cmd/req：" + req);
        mqttBundle.publish(Webtalk.CSN_WSN_pub_cmd_req, req);
        return map;
    }
}
